/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package bbd.crawler.media;

import bbd.crawler.util.Console;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import org.json.JSONArray;
import org.json.JSONObject;

/**
 *
 * @author Harit
 */
public class GoogleCustomSearch extends BaseMediaHandler {

    private static String QUERY = "http://ajax.googleapis.com/ajax/services/search/web?start=START_PAGE&rsz=large&v=1.0&" +
                                  "q=intitle:%22index+of%22(mp3|mp4|avi|flv|mpeg|wma|wmv)" +
                                  "+%22SEARCH_STRING%22+-html+-htm+-asp+-jsp+-cf";

    private static int searchPageNumber = 0;

    private static CopyOnWriteArraySet<String> googleReturnedURLs = new CopyOnWriteArraySet<String>();
    //--------------------------------------------------------------------------
    /** Javadoc here */
    public GoogleCustomSearch () {
        
    } // end default constructor
    
    //--------------------------------------------------------------------------
    /** Javadoc here */
    public static void setQuery ( String string ) {

        QUERY = QUERY.replaceAll( "SEARCH_STRING" , string );

        setQueryString ( string );

        QUERY = QUERY.replaceAll( "START_PAGE" , Integer.toString( searchPageNumber ) );

    } // end function setQuery

    //--------------------------------------------------------------------------
   
    /** Javadoc here */
    public static String query () {

        String readLine = null;

        StringBuffer responseData = new StringBuffer();
        
        System.out.println("Querying .. " + QUERY);

        try {

            URL url = new URL ( QUERY );

            URLConnection connection = url.openConnection();

            BufferedReader reader = new BufferedReader ( new InputStreamReader (connection.getInputStream() ) );

            while ( ( readLine = reader.readLine() ) != null ) {

                responseData.append( readLine );

            }

            return responseData.toString();
            
        } catch ( Exception e ) {

            Console.error( "GoogleCustomSearch.query",  "Java Exception - " + e.getMessage() );

            return null;
            
        }

    } // end function query

    //--------------------------------------------------------------------------
    /** Javadoc here */
    public static void processGoogleResults ( String data ){

        try {

            JSONObject jsonObject = new JSONObject ( data );

            //System.out.println("Total results = " + jsonObject.getJSONObject("responseData").getJSONObject("cursor").getString("estimatedResultCount"));

            JSONArray jsonArray = jsonObject.getJSONObject( "responseData" ).getJSONArray( "results" );

            for ( int i = 0; i < jsonArray.length(); i ++ ) {

                JSONObject entry = jsonArray.getJSONObject( i );

                googleReturnedURLs.add( entry.getString( "url" ) );
                
            }

            while ( searchPageNumber < 16 ) {

                /**
                 * This is used to search upto
                 * 3 result pages of google
                 */
                searchPageNumber += 8;

                QUERY = QUERY.replaceAll("start=\\d", "start="+searchPageNumber);

                String responseData = query ();

                processGoogleResults ( responseData );

            }

        } catch ( Exception e ) {

            Console.error( "GoogleCustomSearch.process", "Java Exception - " + e.getMessage() );
            
        }

    } // end function process

    //--------------------------------------------------------------------------
    /** Javadoc here */
    public static void findMediaUrls () {

        int count = 1;

        Iterator iter = googleReturnedURLs.iterator();

        while ( iter.hasNext() ) {

            System.out.println(count + " : " + iter.next() );

            count ++;
        } 

        getUrls ( googleReturnedURLs, getQueryString() );

        Set<String> audioData = BaseMediaHandler.audioUrls;
        Set<String> videoData = BaseMediaHandler.videoUrls;

        Iterator aIter = audioData.iterator();
        System.out.println("Audio Data now");
        while ( aIter.hasNext() ) {

            System.out.println(aIter.next() + " | ");

        }

        Iterator vIter = videoData.iterator();
        System.out.println("Video Data now");
        while ( vIter.hasNext() ) {

            System.out.println( vIter.next() + " | ");
        }
    }

} // end class GoogleCustomSearch
